<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>contextmenu</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .container {
      width: 200px;
      background: #ccc;
      border-radius: 5px;
      display: none;
      position: fixed;
    }
    .container ul {
      list-style: none;
    }
    .container ul li {
      height: 40px;
      text-align: center;
      line-height: 40px;
      border-radius: 5px;
    }
    .container ul li:hover {
      background: red;
      color: #fff;
    }
  </style>
</head>
<body>
  <div class="container">
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
    </ul>
  </div>
  <script>
    const container = document.getElementsByClassName('container')[0]
    document.addEventListener('contextmenu',(e) => {
      e.preventDefault()
      container.style.left = e.clientX + 20 + 'px'
      container.style.top = e.clientY + 10 + 'px'
      container.style.display = 'block'
    })
    document.addEventListener('click', () => {
      container.style.display = 'none'
    })
  </script>
</body>
</html>